﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

using System.Xml.Linq;
using System.Data;

public partial class DisplaySurvey : System.Web.UI.Page
{
    XmlDocument xmlDoc = new XmlDocument();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDataFromXml();
        }
        lblTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

    }
    public void bindDataFromXml()
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(Server.MapPath("~/xml/survey.xml"));
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        xmlDoc.Load(Server.MapPath("~/xml/result.xml"));
            XmlElement user = xmlDoc.CreateElement("user");

            int questionId = 1;
            XmlNodeList xmlNL = xmlDoc.GetElementsByTagName("user");
            int userId = xmlNL.Count + 1;
            XmlAttribute uId = xmlDoc.CreateAttribute("userID");
            
            uId.Value = userId.ToString();
            foreach (RepeaterItem rItem in Repeater1.Items)
            {
                XmlElement question = xmlDoc.CreateElement("question");
                XmlAttribute quesId = xmlDoc.CreateAttribute("quesId");
                

                for (int i = 1; i <= 4; i++)
                {
                    CheckBox cb = (CheckBox)rItem.FindControl("cbAns" + i.ToString());
                    if (cb.Checked)
                    {
                        question.InnerText += (i - 1).ToString() + ",";
                        quesId.Value = questionId.ToString();
                    }
                }
                question.Attributes.Append(quesId);
                user.AppendChild(question);
                questionId++;
            }

            user.Attributes.Append(uId);
            xmlDoc.DocumentElement.AppendChild(user);
            xmlDoc.Save(Server.MapPath("~/xml/result.xml"));
            Response.Redirect("InfoSubmit.aspx");

            //Response.Write("<script LANGUAGE='JavaScript' >alert('Submit Successful <\n> Thank you!')</script>");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home2.aspx");
    }
}